<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of xiao mi
 *
 * @author sunday add 2014.5.28 | dragon.hu 2015.11.27 modify
 */
require_once 'BaseChannel.php';

//小米
class Xiaomi implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		//$paymentKey="5241722812633"; 验证逻辑不变，改个paymentKey就OK了
		//$paymentKey="hXAgponCs3OIsKA5giBLZA==";
		$paymentKey="Uu2Huw/LPgnd8PGOLMzEIg==";
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		//$paymentKey="5241722812633"; 
		//$paymentKey="hXAgponCs3OIsKA5giBLZA==";
		$paymentKey="Uu2Huw/LPgnd8PGOLMzEIg==";
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("xiaomi", $_REQUEST);

		//先判断接到的数据是否正确
		if(empty($_REQUEST)||!isset($_REQUEST['appId'])||!isset($_REQUEST['cpOrderId'])||!isset($_REQUEST['uid'])||!isset($_REQUEST['orderId'])
		||!isset($_REQUEST['orderStatus'])||!isset($_REQUEST['payFee'])||!isset($_REQUEST['productCode'])||!isset($_REQUEST['productName'])
		||!isset($_REQUEST['productCount'])||!isset($_REQUEST['payTime'])||!isset($_REQUEST['signature']))
		{
			$result["errcode"] =  "1515";
			$result["errMsg"] =  "data receive error";

			$res = json_encode($result);
			$orderLog->log("xiaomi"," reveive data error, sth is empty ");
			//echo " receive error ";
			return urldecode($res);
		}
		
		$appId = $_REQUEST['appId'];	//必须 	游戏ID
		$cpOrderId = $_REQUEST['cpOrderId'];	//必须 	开发商订单ID
		$cpUserInfo = $_REQUEST['cpUserInfo'];	//可选 	开发商透传信息 
		$uid = $_REQUEST['uid'];	//必须 	用户ID
		$orderId = $_REQUEST['orderId'];	// 	必须 	游戏平台订单ID
		$orderStatus = $_REQUEST['orderStatus'];	// 	必须 	订单状态，TRADE_SUCCESS 代表成功
		$payFee = $_REQUEST['payFee'];	// 	必须 	支付金额,单位为分,即0.01 米币。
		$productCode = $_REQUEST['productCode'];	//必须 	商品代码
		$productName = $_REQUEST['productName'];	// 	必须 	商品名称
		$productCount = $_REQUEST['productCount'];	// 	必须 	商品数量
		$payTime = $_REQUEST['payTime'];	// 	必须 	支付时间,格式 yyyy-MM-dd HH:mm:ss
		$orderConsumeType = $_REQUEST['orderConsumeType'];	// 	可选 	订单类型：10：普通订单11：直充直消订单
		$signature = $_REQUEST['signature'];	//必须 	签名,签名方法见后面说明
		
		/*
		 * 表中各参数按字母顺序排序(不包含 signature),如果第一个字母相同,按第二个字母排序,依次类推。排序后拼接成par1=val1&par2=val2&par3=val3的格式,
		 * 所生成的字符串即为待签名的字符串。没有值的参数请不要参与签名。由于有些数据根据HTTP协议需求,需要进行URLencoding,这样接收方才可以
			接收到正确的参数,但如果这个参数参与签名,那么待签名字符串必须是字符串原值而非URLencoding 的值。
			
			以appKey作为key,使用hmac-sha1带密钥(secret)的哈希算法对代签字符串进行签名计算,签名的结果由16进制表示。
		 */

		//对输入参数根据参数名排序，并拼接为key=value&key=value格式；
		$data = array();

		$data['appId'] = $appId;
		$data['cpOrderId'] = $cpOrderId;
		if (isset($cpUserInfo)) { $data['cpUserInfo'] = $cpUserInfo;	}
		$data['uid'] = $uid;
		$data['orderStatus'] = $orderStatus;
		$data['payFee'] = $payFee;
		$data['productCode'] = $productCode;
		$data['productName'] = $productName;
		$data['productCount'] = $productCount;
		$data['payTime'] = $payTime;
		if (isset($orderConsumeType)) {$data['orderConsumeType'] = $orderConsumeType;}
		$data['signature'] = $signature;

		//对参数按英文大小写排序 拼接
		ksort($data);
		//print_r($data);
		$sourcestr = "";
		
		foreach ($data as $key => $val) 
		{
			$sourcestr == "" ? ($sourcestr = "$key=$val") : ($sourcestr .="&$key=$val");
		}

		/*返回 结果
		 * errcode 	必须 	状态码,200 成功1506 cpOrderId 错误1515 appId 错误1516 uid 错误1525 signature 错误3515 订单信息不一致，用于和 CP 的订单校验
			errMsg 	可选 	错误信息
		 */
		//验证签名
		$signResult = hash_hmac('sha1', $sourcestr, $paymentKey);
		if (true == $signResult)	
		{
			if($orderStatus == 'TRADE_SUCCESS')
			{
				$status = STATUS_SUCCESS;
			}else 
			{
				$status = STATUS_FAILD;
			}
			//收到 的钱是分为单位。。
			if(true == $orderLog->recordOrder($status, $payFee/100, $cpOrderId, "xiaomi", $platform,$orderId))
			{
				$orderLog->log('xiaomi','platform:'.$platform.'amount:'.($payFee/100).' order_id:'.$cpOrderId.' success');
				$result["errcode"] =  "200";
				$result["errMsg"] =  "success";
	        }else 
	        { 			
				$orderLog->log('xiaomi','platform:'.$platform.'amount:'.($payFee/100).' order_id:'.$cpOrderId.' fail');
				$result["errcode"] =  "1515";
				$result["errMsg"] =  "insert sql error";
	        }
		} else 
		{
			//echo " order:".$json['order_id_com']." sign verify fail";
			$orderLog->log('xiaomi'," order:".$cpOrderId." sign verify fail");
			$result["errcode"] =  "3515";
			$result["errMsg"] =  "sign error";
		}	
		$res = json_encode($result);
		//print_r($result);
		return urldecode($res);		
	}	
}